﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using UIKit.Common;
using System.Collections;

namespace UIKit.Controls
{
    [TemplatePart(Name=ListScrollerName,Type=typeof(ScrollViewer))]
    [TemplatePart(Name = ItemsPanelName, Type = typeof(Canvas))]
    public class ImageWallContainer : SuperItemsControl
    {
        #region Const
        private const string ListScrollerName = "ListScroller";
        private const string ItemsPanelName = "ItemsPanel";
        #endregion

        #region Fields
        protected ScrollViewer _listScroller;
        protected Canvas _itemsPanel;

        protected ImageWallCaculator _imageWallCaculator;

        private GestureListener _gestureListener;

        private double _oldVertical;
        private Orientation _direction;
        private double _verticalChange;
        #endregion

        #region Event
        public event VerticalScrollChangedEventHandler VerticalScrollChanged;
        #endregion

        public ImageWallContainer()
        {
            this.DefaultStyleKey = typeof(ImageWallContainer);
            _imageWallCaculator = new ImageWallCaculator();
            this.VerticalScrollChanged += ContainerVerticalScrollChanged;
        }

        void ContainerVerticalScrollChanged(object sender, VerticalScrollChangedEventArgs e)
        {
            double verticalOffset = _listScroller.VerticalOffset;
            double monitorScreenHeight = _listScroller.ExtentHeight - verticalOffset;
            double screenHeight = _listScroller.ViewportHeight;

            if (monitorScreenHeight > screenHeight && Math.Abs(_oldVertical - verticalOffset) > 100)
            {
                VirtualizingData(verticalOffset);
            }
            _oldVertical = verticalOffset;
            if (_listScroller.VerticalOffset >= _listScroller.ScrollableHeight - 200 && _verticalChange <= 15)
            {
                PullDownToBottom();
            }
        }

        #region Override
        
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _listScroller = (ScrollViewer)GetTemplateChild(ListScrollerName);
            _itemsPanel = (Canvas)GetTemplateChild(ItemsPanelName);
            if (_listScroller != null)
            {
                this.SetBinding(DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), this.GetType(), new PropertyMetadata((d, e) =>
                {
                    if (VerticalScrollChanged != null)
                        VerticalScrollChanged(_listScroller, new VerticalScrollChangedEventArgs(_listScroller.VerticalOffset));
                })), new Binding("VerticalOffset") { Source = this._listScroller });

                _gestureListener = GestureService.GetGestureListener(_listScroller);
                _gestureListener.DragDelta += GestureDragDelta;
                _gestureListener.DragCompleted += GestrueDragCompleted;
            }
            if (_itemsPanel != null)
            {
                _imageWallCaculator.ItemsPanel = _itemsPanel;
            }
            
        }
        
        void GestureDragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            _direction = e.Direction;
            _verticalChange = e.VerticalChange;
            if (e.VerticalChange > 100 && _listScroller.VerticalOffset == 0.0)
            {
                BeginPullUp();
            }
        }

        void GestrueDragCompleted(object sender, DragCompletedGestureEventArgs e)
        {
            PullUpToTop();            
        }

        #endregion

        #region VirtualMethod

        protected virtual void VirtualizingData(double verticalOffset)
        {

        }

        protected virtual void PullDownToBottom()
        {

        }

        protected virtual void BeginPullUp()
        {

        }

        protected virtual void PullUpToTop()
        {
            
        }
        #endregion
    }
}
